import { isPoneAvailable } from '../../../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    detailed: '',
    addressDetails: '',
    addressArr: [],
    province: '',   //省
    city: '',    //市
    county: '',    //区
    isSelect: false, // 是否选择地址
    currentUserId: '', // 当前用户id
    activityId: '', // 当前活动id

    btnAntis: true, // 按钮防抖
    btnNum: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const { activityId } = app.globalData.activityData
    const { currentUserId } = options
    this.setData({
      activityId, // 字符串id
      currentUserId // 当前用户id
    })
  },

  /**
   * @see 点击按钮提交
   */
  handleClick () {
    const {
      name, // 用户名
      phone, // 手机号
      detailed, // 地址
      addressDetails, // 详细地址
      currentUserId, // 当前用户id
      activityId, // 活动id
      province,   //省
      city,    //市
      county,    //区
      btnNum,
    } = this.data
    if (!name.trim() || !phone || !addressDetails.trim() || !detailed.trim()) {
      wx.showToast({
        title: '请填全信息',
        icon: 'none'
      })
      return
    }
    if (!isPoneAvailable(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    if (btnNum > 0) { return }
    this.data.btnNum += 1;
    let request = {
      province,   //省
      city,    //市
      area: county,    //区
      organizeId: currentUserId, // 用户id
      consignee: name,
      telephone: phone,
      address: addressDetails,
    }
    wx.$API['eventConfiguration/addManageAddress'](request).then(res => {
      this.exchangeMask()
    })
      .catch(error => {
        wx.hideLoading()
        --this.data.btnNum
        wx.showToast({
          title: error,
          icon: 'none'
        })
        console.log('eventConfiguration/addManageAddress 提交地址错误!', error)
      })
    return
  },
  /**
   * @see 立即兑换口罩
   */
  exchangeMask () {
    const { activityId, currentUserId } = this.data
    const request = {
      activityId, // 活动id
      id: currentUserId, // 用户id
    }
    if (!this.data.btnAntis) { return }
    this.data.btnAntis = false
    // 调用立即兑换口罩接口
    wx.$API['eventConfiguration/exchange'](request)
      .then(res => {
        wx.hideLoading()
        this.addData()
        wx.showToast({
          title: res,
          icon: 'success'
        })
        this.setData({
          status: 1,
          btnAntis: true
        })
        wx.redirectTo({
          url: `/pages/eventConfiguration/pages/prizeCollection/index?id=${currentUserId}`
        })
      })
      .catch(error => {
        this.data.btnAntis = true
        wx.showToast({
          title: error,
          icon: 'none'
        })
        console.log('eventConfiguration/exchange 兑换口罩错误!', error)
      })
  },
  /**
   * @see 输入框改变事件
   */
  inputChange (e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail.value
    })
  },
  /**
   * @see 地址选择器发生变化
   */
  bindRegionChange (e) {
    let str = ''
    e.detail.value.forEach(item => {
      str += ' ' + item
    })
    this.setData({
      addressArr: e.detail.value,
      detailed: str,
      isSelect: true,
      province: e.detail.value[0],
      city: e.detail.value[1],
      county: e.detail.value[2]
    })
  },
  /**
   * @see 数据统计
   */
  addData () {
    const { activityId } = this.data
    const request = {
      activityId,
      dataType: 3
    }
    wx.$API['eventConfiguration/addData'](request).then(res => {
      console.log('数据统计 -- 兑换次数加一,成功');
    })
      .catch(error => {
        console.log('数据统计 -- 兑换次数加一,错误!', error);
      })
  },
})